package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    UserService service;

    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    @RequestMapping("/getUsername")
    public Result findUsername() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                if ("ROLE_ADMIN".equals(authority.toString())) {
                    return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user);
                }
            }
            return new Result(false, MessageConstant.GET_USERNAME_FAIL, user);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }


    /**
     * 分页
     *
     * @param bean
     * @return
     */
    @RequestMapping("/findByPage.do")
    public PageResult findPage(@RequestBody QueryPageBean bean) {
        PageResult result = service.findPage(bean);
        return result;
    }
    @RequestMapping("/findById.do")
    public Result findById(String id) {
        List<Role> list = service.findById(id);
        if (list!=null&&list.size()>0){
            return new Result(true,"",list);
        }
        return new Result(false,"已拥有全部角色！");
    }

    /**
     * 添加角色
     *
     * @param id
     * @param roleIds
     * @return
     */
    @RequestMapping("/addRole.do")
    public Result add(Integer id, Integer[] roleIds) {
        try {
            service.addRole(id, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    //添加用户
    @RequestMapping("/addUser.do")
    public Result addUser(@RequestBody com.itheima.pojo.User user) {
        try {
            user.setPassword( passwordEncoder.encode(user.getPassword()));
            service.addUser(user);
            return new Result(true, "用户添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "用户添加失败");
        }
    }


    //删除用户
    @RequestMapping("/del.do")
    public void del(Integer id) {
        service.del(id);
    }

}
